package ypmits.utils {
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.geom.Rectangle;

	/**
	 * @author Dave Lenz
	 */
	public class BoxFitter extends Sprite {
		public static function sizer( obj : DisplayObject, rect : Rectangle ) : void
		{
			obj.height = rect.height;
			obj.scaleX = obj.scaleY;
			if( obj.width < rect.width ) {
				obj.width = rect.width;
				obj.scaleY = obj.scaleX;
			}
			obj.x = rect.width * .5;
			obj.y = rect.height * .5;
		}



		public static function sizerTopLeft( obj : DisplayObject, width : Number, height : Number, center : Boolean ) : void
		{
			if( obj ) {
				obj.height = height;
				obj.scaleX = obj.scaleY;
				if( obj.width < width ) {
					obj.width = width;
					obj.scaleY = obj.scaleX;
				}
				if( center ) {
					(obj.width - width) >= 0 ? obj.x = -(( obj.width - width ) * .5) : obj.x = 0;
					(obj.height - height) >= 0 ? obj.y = -(( obj.height - height ) * .5) : obj.y = 0;
				}
			}
		}



		public static function centerFit( obj : DisplayObject, width : Number, height : Number ) : void
		{
			if( !obj )
				return;
			obj.height = height;
			obj.scaleX = obj.scaleY;

			if( obj.width > width ) {
				obj.width = width;
				obj.scaleY = obj.scaleX;
			}
			if( obj.height > height ) {
				obj.height = height;
				obj.scaleX = obj.scaleY;
			}
			obj.x = (width - obj.width ) * .5;
			obj.y = (height - obj.height ) * .5;

		}
	}
}
